package org.ngmj.demo1.severclientread;


import lombok.extern.slf4j.Slf4j;

import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;

@Slf4j
public class Client {
    public static void main(String[] args) {
        try (SocketChannel sc = SocketChannel.open()) {
            sc.connect(new java.net.InetSocketAddress("127.0.0.1", 8080));
            sc.write(Charset.defaultCharset().encode("hello\n"));
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNext()) {
                String next = scanner.next();
                log.info("write: {}", next);
                sc.write(Charset.defaultCharset().encode(next + "\n"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
